from rest_framework import serializers
from .models import *


class AdvertSer(serializers.ModelSerializer):
    class Meta:
        model = Advert
        fields = "__all__"


class CategorySer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = "__all__"


class CategoryGroupSer(serializers.ModelSerializer):
    cate_list = serializers.SerializerMethodField()

    cate2_list = serializers.SerializerMethodField()

    def get_cate2_list(self, group):
        queryset = Category.objects.filter(group_id=group.id, level=2)
        return CategorySer(queryset, many=True).data

    def get_cate_list(self, group):
        # 根据组的ID去查找 分类  看看这个分组下 有哪些 分类
        queryset = Category.objects.filter(group_id=group.id, level=1)

        return CategorySer(queryset, many=True).data

    class Meta:
        model = CategoryGroup
        fields = "__all__"


# 商品序列号器
class GoodsSer(serializers.ModelSerializer):
    class Meta:
        model = Goods
        fields = "__all__"


# 商品详情序列化
class GoodsInfoSer(serializers.ModelSerializer):
    category1 = serializers.SlugRelatedField(slug_field="name", read_only=True)
    category2 = serializers.SlugRelatedField(slug_field="name", read_only=True)

    class Meta:
        model = Goods
        fields = "__all__"


class CategorySer1(serializers.ModelSerializer):
    goods_list = serializers.SerializerMethodField()

    def get_goods_list(self, obj):
        goods_queryset = Goods.objects.filter(category1_id=obj.id)[:10]

        return GoodsSer(goods_queryset, many=True).data

    class Meta:
        model = Category
        fields = "__all__"
